<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Book_Category extends Controller{

public function action_index()
{
}

public function action_list()
{
	$query_result = ORM::factory('category')->order_by('order')->find_all();
	$result = $this->create_category_list($query_result->as_array());
	ob_start("ob_gzhandler"); 
	echo json_encode($result);
}

protected function create_category_list($array)
{
	$object = array();
	foreach ( $array as $category)
	{
		array_push($object,
			array(
				'id'	=> $category->id,
				'name'  => $category->name,
				'image' => '/uploads/category/'.$category->image
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}

public function action_image()
{
	$id = $this->request->param('id');
	if ($id != NULL)
	{
		$category = ORM::factory('category')->where('id', '=', $id)->find();
		if ($category->loaded())
		{
			Header("Content-type:image/pjpeg");
			echo $category->image;
		}
	}
	else
	{
		echo "无id";
	}
}

}


